package com.zzxy.spring.aspect;

import com.zzxy.spring.service.IUserService;
import com.zzxy.spring.service.impl.UserServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author aven
 * @description: TODO
 */
public class MyBeanFactory {
    public static IUserService createUserService() {
        //1.target
        final UserServiceImpl userService = new UserServiceImpl();
        //2.advice
        final MyAspect aspect = new MyAspect();
        //3.weaving => aop
        IUserService userServieProxy = (IUserService) Proxy.newProxyInstance(MyBeanFactory.class.getClassLoader(), userService.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                aspect.before();
                Object obj = method.invoke(userService, args);
                aspect.after();
                return obj;
            }
        });
        return userServieProxy;
    }
}
